package com.gxuwz.weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gxuwz.weather.R;

import java.util.List;
import java.util.Map;

public class CitySearchAdapter extends BaseAdapter {
    private Context context;
    private List<Map<String, String>> data;
    private LayoutInflater inflater;

    public CitySearchAdapter(Context context, List<Map<String, String>> data) {
        this.context = context;
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_city_search, parent, false);
            holder = new ViewHolder();
            holder.tvCountyName = convertView.findViewById(R.id.tv_county_name);
            holder.tvCityName = convertView.findViewById(R.id.tv_city_name);
            holder.tvAreaId = convertView.findViewById(R.id.tv_area_id);
            holder.tvAreaCode = convertView.findViewById(R.id.tv_area_code);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Map<String, String> cityInfo = data.get(position);
        holder.tvCountyName.setText(cityInfo.get("areaCn"));
        holder.tvCityName.setText(cityInfo.get("cityCn"));
        holder.tvAreaId.setText(cityInfo.get("areaId"));
        holder.tvAreaCode.setText(cityInfo.get("areaCode"));

        return convertView;
    }

    private static class ViewHolder {

        TextView tvCountyName;
        TextView tvCityName;
        TextView tvAreaId;
        TextView tvAreaCode;
    }
}
